package mycoffee.order.dialog;

import mycoffee.order.R;
import mycoffee.order.model.Discount;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.RadioButton;
import android.widget.PopupMenu.OnMenuItemClickListener;

public class DiscountDialog extends Dialog implements OnClickListener {

    private EditText mEtName = null;
    private EditText mEtValue = null;
    private Discount mDiscount;
    private RadioButton mRbPercent;
    private RadioButton mRbMoney;
    private ImageView mIvDiscoutValues;

    public DiscountDialog(Context context) {
        super(context);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.discount_dialog);

        mEtName = (EditText) findViewById(R.id.discount_dialog_et_name);
        mEtValue = (EditText) findViewById(R.id.discount_dialog_et_value);
        mRbPercent = (RadioButton) findViewById(R.id.discount_dialog_rb_percent);
        mRbPercent.setOnClickListener(this);

        mRbMoney = (RadioButton) findViewById(R.id.discount_dialog_rb_money);
        mRbMoney.setOnClickListener(this);

        ImageView mIvDelete = (ImageView) findViewById(R.id.discount_dialog_iv_delete);
        mIvDelete.setOnClickListener(this);
        Button btOk = (Button) findViewById(R.id.discount_dialog_bt_ok);
        btOk.setOnClickListener(this);
        Button btCancel = (Button) findViewById(R.id.discount_dialog_bt_cancel);
        btCancel.setOnClickListener(this);

        mIvDiscoutValues = (ImageView) findViewById(R.id.discount_dialog_iv_values);
        mIvDiscoutValues.setOnClickListener(this);

        ImageView ivNames = (ImageView) findViewById(R.id.discount_dialog_iv_names);
        ivNames.setOnClickListener(this);
        mRbPercent.setChecked(true);

        super.onCreate(savedInstanceState);
    }

    public void resetDialog() {
        mDiscount = null;
        mRbPercent.setChecked(true);
        mEtName.setText("");
        mEtValue.setText("");
        mIvDiscoutValues.setVisibility(View.VISIBLE);
    }

    @Override
    public void onClick(View v) {
        if (v == null) {
            return;
        }
        switch (v.getId()) {
        case R.id.discount_dialog_bt_ok:
            mDiscount = new Discount();
            mDiscount.setName(mEtName.getText().toString());
            String value = mEtValue.getText().toString();
            if (!TextUtils.isEmpty(value)) {
                if (mRbMoney.isChecked()) {
                    mDiscount.setMoney(Double.parseDouble(value));
                } else {
                    mDiscount.setPercent(Integer.parseInt(value));
                }
            }
            dismiss();
            break;
        case R.id.discount_dialog_bt_cancel:
        case R.id.discount_dialog_iv_delete:
            dismiss();
            break;
        case R.id.discount_dialog_iv_names:
            showPopupMenuDiscountNames();
            break;
        case R.id.discount_dialog_iv_values:
            showPopupMenuDiscountValues();
            break;
        case R.id.discount_dialog_rb_percent:
            mIvDiscoutValues.setVisibility(View.VISIBLE);
            break;
        case R.id.discount_dialog_rb_money:
            mIvDiscoutValues.setVisibility(View.GONE);
            break;
        default:
            break;
        }
    }

    /**
     * show popup menu to choose discount value.
     */
    private void showPopupMenuDiscountValues() {
        final PopupMenu popupMenu = new PopupMenu(getContext(), mEtValue);
        int menuRes = R.menu.menu_discount_values;
        popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                mEtValue.setText(item.getTitle());
                return true;
            }
        });
        popupMenu.show();
    }

    /**
     * show popup menu to choose discount name.
     */
    private void showPopupMenuDiscountNames() {
        final PopupMenu popupMenu = new PopupMenu(getContext(), mEtName);
        int menuRes = R.menu.menu_discount_names;
        popupMenu.getMenuInflater().inflate(menuRes, popupMenu.getMenu());
        popupMenu.setOnMenuItemClickListener(new OnMenuItemClickListener() {

            @Override
            public boolean onMenuItemClick(MenuItem item) {
                mEtName.setText(item.getTitle());
                return true;
            }
        });
        popupMenu.show();
    }

    public Discount getDiscount() {
        return mDiscount;
    }
}
